package com.lal.springboot.controller;

import com.lal.sdk.model.LalResponse;
import com.lal.sdk.util.IpUtil;
import com.lal.springboot.service.LalServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/lalserver")
public class LalServerController {

    @Autowired
    private LalServerService lalServerService;

    @PostMapping("/start-pull")
    public Map<String, Object> startPullStream(@RequestBody Map<String, String> request) {
        String streamName = request.get("streamName");
        String rtspUrl = request.get("rtspUrl");
        
        LalResponse response = lalServerService.startPullStream(streamName, rtspUrl);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", response.isSuccess());
        result.put("code", response.getCode());
        result.put("message", response.getMessage());
        result.put("data", response.getData());
        
        return result;
    }

    @PostMapping("/stop-pull")
    public Map<String, Object> stopPullStream(@RequestBody Map<String, String> request) {
        String streamName = request.get("streamName");
        LalResponse response = lalServerService.stopPullStream(streamName);
        Map<String, Object> result = new HashMap<>();
        result.put("success", response.isSuccess());
        result.put("code", response.getCode());
        result.put("message", response.getMessage());
        result.put("data", response.getData());
        
        return result;
    }

    @GetMapping("/status/{streamName}")
    public Map<String, Object> getStreamStatus(@PathVariable String streamName) {
        LalResponse response = lalServerService.getStreamStatus(streamName);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", response.isSuccess());
        result.put("code", response.getCode());
        result.put("message", response.getMessage());
        result.put("data", response.getData());
        
        return result;
    }

    @GetMapping("/urls/{streamName}")
    public Map<String, Object> getStreamUrls(@PathVariable String streamName, HttpServletRequest request) {
        String clientIp = IpUtil.getRequestIp(request);
        Map<String, Object> urls = new HashMap<>();
        urls.put("flv", lalServerService.getSecureFlvUrl(streamName, clientIp));
        urls.put("hls", lalServerService.getSecureHlsUrl(streamName,clientIp));
        urls.put("webrtc", lalServerService.getSecureWebRtcUrl(streamName, clientIp));
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", urls);
        return result;
    }

    @PostMapping("/validate-token")
    public Map<String, Object> validateToken(@RequestBody Map<String, String> request) {
        String token = request.get("token");
        String streamName = request.get("streamName");
        String clientIp = request.get("clientIp");
        
        boolean isValid = lalServerService.validateStreamAccess(token, streamName, clientIp);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("valid", isValid);
        
        return result;
    }

    @PostMapping("/revoke-token")
    public Map<String, Object> revokeToken(@RequestBody Map<String, String> request) {
        String token = request.get("token");
        
        lalServerService.revokeStreamAccess(token);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "Token revoked successfully");
        
        return result;
    }

    @PostMapping("/cleanup")
    public Map<String, Object> cleanupExpiredSessions() {
        lalServerService.cleanupExpiredSessions();
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "Expired sessions cleaned up");
        
        return result;
    }
}